<template>
    <div class="view-main">
        <div class="view-header">
            <div>
            </div>
            <el-button type="primary" size="small" @click="handleAdd">添加</el-button>
        </div>
        <div class="view-body">
            <div class="light-table">
                <el-table :data="tableData" height="300" highlight-current-row style="width: 100%">
                    <el-table-column property="name" label="案卷类型" align="left" show-overflow-tooltip></el-table-column>
                    <el-table-column property="remark" label="备注" align="left" show-overflow-tooltip></el-table-column>

                    <el-table-column label="操作" align="left" width="330">
                        <template slot-scope="scope">
                            <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                            <el-button type="text" class="danger-color" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--:page-size.sync="pageSize"-->
                <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                               :page-sizes="[10, 20, 30, 40]" :total="total" :current-page="pageNum" :page-size="pageSize"
                               :page-count="pages" @current-change="handlePageChange" @size-change="handleSizeChange">

                </el-pagination>
            </div>
        </div>

        <el-dialog class="no-bottom-padding user-dialog" :title="typeText" :visible.sync="dialogVisible" width="480px" :close-on-click-modal="false">
            <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="80px" size="mini" style="padding-right: 20px" :disabled="type === 2">
                <el-form-item label="案卷类型" prop="name">
                    <el-input v-model="addForm.name" clearable></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model.number="addForm.remark" clearable></el-input>
                </el-form-item>
            </el-form>
            <!--编辑页面-->
            <div slot="footer" class="dialog-footer" v-if="type < 2">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateConfirm('addForm')">确 定</el-button>
        </div>
        </el-dialog>

    </div>
</template>

<script>
    import { checkLoginName, checkPhone, checkEmail, checkObj, isEmpty, stringFormat } from '../../utils/util'
    import { getAction,putAction, downloadAction } from '@/utils/manage'

    export default {
        name: 'user',
        data() {
            return {
                tableData: [],
                //当前页数
                pageNum: 1,
                pageSize: 10,
                pages: 0,
                total: 0,
                type: 0,
                dialogVisible: false,
                addRules: {
                    name: [{ required: true, message: '请输入案卷类型', trigger: 'blur' }],
                    remark: [{ required: true, message: '请输入备注', trigger: 'blur' }],
                },
                roleList: [],
                addForm: {
                    id: '',
                    name: '',
                    remark: '',
                },
                url: {
                    // page: '/sys/user',
                    page: '/jzm/jzFileType',
                    update: '/jzm/jzFileType/update/{0}',
                    insert: '/jzm/jzFileType/insert',
                    delete: '/jzm/jzFileType/delete/{0}',
                }
            }
        },
        computed: {
            typeText() {
                return this.type === 0 ? '新增' : this.type === 1 ? '修改' : '详情'
            }
        },
        watch: {},
        created() {
            this.getDataList(1)
        },
        methods: {
            handleDetail(row) {
                this.addForm = JSON.parse(JSON.stringify(row))
                this.type = 2
                this.dialogVisible = true
                if (!isEmpty(this.$refs.addForm)) {
                    this.$refs.addForm.resetFields()
                }
            },
            handleUpdate(row) {
                this.addForm = JSON.parse(JSON.stringify(row))
                this.type = 1
                this.dialogVisible = true
                if (!isEmpty(this.$refs.addForm)) {
                    this.$refs.addForm.resetFields()
                }
            },
            handleDelete(row) {
                this.$confirm('确认删除?', '提示', { type: 'warning' })
                    .then(() => {
                        if (!isEmpty(row) && !isEmpty(row.id)) {
                            var url = stringFormat(this.url.delete, [row.id])
                            // console.log(url)
                            this.$http.delete(url).then(() => {
                                this.getDataList()
                            })
                        }
                    })
                    .catch(() => {})
            },
            //获取数据分页展示
            getDataList() {
                let params = { _pageSize: this.pageSize, _pageNum: this.pageNum, ...this.queryParam }
                getAction(this.url.page, params).then(data => {
                    // console.log(data)
                    let result = data.result
                    // console.log(result)
                    this.tableData = result.records
                    this.pageNum = result.current
                    this.pageSize = result.size
                    this.pages = result.pages
                    this.total = result.total
                })
            },
            // 监听页码值 改变的事件
            handlePageChange(newPage) {
                // console.log(newPage)
                this.pageNum = newPage
                this.getDataList(newPage)
            },
            // 监听pageSize改变的事件
            handleSizeChange(newSize) {
                // console.log(newSize)
                this.pageSize = newSize
                this.getDataList()
            },
            handleAdd() {
                this.type = 0
                this.addForm = {
                    name: '',
                    remark: '',
                }
                this.dialogVisible = true
                if (!isEmpty(this.$refs.addForm)) {
                    this.$refs.addForm.resetFields()
                }
            },
            updateConfirm() {
                this.$refs.addForm.validate(valid => {
                    if (valid) {
                        let _form = JSON.parse(JSON.stringify(this.addForm))

                        /* insert */
                        if (this.type === 0) {
                            var address = this.url.insert
                            this.$http.put(address, _form).then(() => {
                                // console.log(this.$http.put(address, _form).then())
                                // console.log(res)
                                this.getDataList()
                            })
                            /* update */
                        } else {
                            var address = stringFormat(this.url.update, [_form.id])
                            // console.log(address)
                            this.$http.put(address, _form).then(() => {
                                this.getDataList()
                            })
                        }


                        this.dialogVisible = false
                    } else {
                        return false
                    }
                })
            }
        }
    }
</script>
<style scoped></style>
